/*
 * cd.scala
 */

package acorn.jash.commands

import acorn.jash.Cmd
import acorn.jash.Shell._
import acorn.jash.Path._
import acorn.jash.JashException
  
class cd extends Cmd {
  override def exec {

    debug("args.length => " + args.length)
    debug(if (args.length > 0) "args(0) => " + args(0) else "")
  
    val t = args.length match {
      case 0 => home
      case _ => if (args(0) == "-") lwd
                else wd changeTo args(0)
    }
    if (t.asFile.isDirectory) {
      lwd = wd; wd = t
    }
    else
      throw new NotDirectoryException(t.realpath)
  }
}

class NotDirectoryException(fn:String)
  extends JashException("[" + fn + "] is not a directory")
